package com.zzyl.serve.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.domain.NursingProjectPlan;
import com.zzyl.serve.domain.dto.NursingPlanDto;
import com.zzyl.serve.domain.vo.NursingPlanVo;
import com.zzyl.serve.domain.vo.NursingProjectPlanVo;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.mapper.NursingProjectMapper;
import com.zzyl.serve.service.INursingPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * 护理计划Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-04
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    /**
     * 查询护理计划
     * 
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Long id)
    {
        //查询nursingPlan
        NursingPlanVo nursingPlanVo = new NursingPlanVo();
        BeanUtil.copyProperties(getById(id), nursingPlanVo);

        //查询nursingPlan对应的nursingProject
        List<NursingProjectPlan> nursingProjectPlans = Db.lambdaQuery(NursingProjectPlan.class)
                .eq(NursingProjectPlan::getPlanId, id)
                .list();
        if(nursingProjectPlans == null) {
            return nursingPlanVo;
        }
        nursingPlanVo.setProjectPlans(BeanUtil.copyToList(nursingProjectPlans, NursingProjectPlanVo.class));
        return nursingPlanVo;
    }

    /**
     * 查询护理计划列表
     * 
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     * 
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertNursingPlan(NursingPlanDto nursingPlanDto)
    {
        //插入护理计划表
        NursingPlan nursingPlan = BeanUtil.copyProperties(nursingPlanDto, NursingPlan.class);
        save(nursingPlan);
        Long id = nursingPlan.getId();
        //插入护理计划项目表
        List<NursingProjectPlan> nursingProjectPlan = nursingPlanDto.getProjectPlans();
        nursingProjectPlan.forEach(e -> {
            e.setPlanId(id);
            Db.save(e);
        });
        return 1;
    }

    /**
     * 修改护理计划
     * 
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateNursingPlan(NursingPlanDto nursingPlanDto)
    {
        //删除护理计划以及对应的护理项目
        updateById(BeanUtil.copyProperties(nursingPlanDto, NursingPlan.class));
        Db.remove(Wrappers.lambdaQuery(NursingProjectPlan.class)
                .eq(NursingProjectPlan::getPlanId, nursingPlanDto.getId()));

        //重新插入护理计划和项目
        List<NursingProjectPlan> nursingProjectPlan = nursingPlanDto.getProjectPlans();
        nursingProjectPlan.forEach(e -> {
            e.setPlanId(nursingPlanDto.getId());
            Db.save(e);
        });
        return 1;
    }

    /**
     * 批量删除护理计划
     * 
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteNursingPlanByIds(Long[] ids)
    {
        //删除所有护理计划项目数据
        Db.remove(Wrappers.lambdaQuery(NursingProjectPlan.class)
                .in(NursingProjectPlan::getPlanId, ids));

        //删除护理计划
        removeByIds(Arrays.asList(ids));
        return 1;
    }

    /**
     * 删除护理计划信息
     * 
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Long id)
    {
        return removeById(id) ? 1 : 0;
    }
}
